package www.openteam.com.adapter.other;

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.util.SortedList;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import java.util.ArrayList;

import www.openteam.com.R;
import www.openteam.com.bean.PeopleGroup;


/**
 * Created by kylin on 2016/8/19.
 */
public class WOSearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 人组Adapter
     */
    private static final String TAG = WOSearchAdapter.class.getSimpleName();
    private LayoutInflater mLayoutInflater;
    private SortedList<PeopleGroup.DataEntity> mSortedList;
    private Context context;
    private StringBuffer sb = new StringBuffer();
    private CButtonTouchEvent cButtonTouchEvent;

    public WOSearchAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(PeopleGroup.DataEntity.class, new SortedList.Callback<PeopleGroup.DataEntity>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(PeopleGroup.DataEntity o1, PeopleGroup.DataEntity o2) {

                if (o1.getRownumber() < o2.getRownumber()) {
                    return -1;
                } else if (o1.getRownumber() > o2.getRownumber()) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(PeopleGroup.DataEntity oldItem, PeopleGroup.DataEntity newItem) {
                return oldItem.getGroupcount().equals(oldItem.getGroupcount());
            }

            @Override
            public boolean areItemsTheSame(PeopleGroup.DataEntity item1, PeopleGroup.DataEntity item2) {
                boolean statue;
                if (item1.getGroupcount().equals(item2.getGroupcount())
                        ) {
                    statue = true;
                } else {
                    statue = false;
                }
                return statue;
            }

            @Override
            public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
    }

    public void addItems(ArrayList<PeopleGroup.DataEntity> list) {
        mSortedList.beginBatchedUpdates();
        for (PeopleGroup.DataEntity itemModel : list) {
            mSortedList.add(itemModel);
        }
        mSortedList.endBatchedUpdates();
    }

    public void addContext(Context context) {
        this.context = context;
    }

    public void deleteItems(ArrayList<PeopleGroup.DataEntity> items) {
        mSortedList.beginBatchedUpdates();
        for (PeopleGroup.DataEntity item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.d(TAG, "DataAdapter: 3");
        return new WOSearchAdapter.pViewHolder(
                mLayoutInflater.inflate(R.layout.item_base_banzu, parent, false), context);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//        Log.d(TAG, "DataAdapter:4");
        final PeopleGroup.DataEntity item = mSortedList.get(position);
        pViewHolder viewHolder = (pViewHolder) holder;
        viewHolder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.red_shallow));
        viewHolder.textView.setText(context.getString(R.string.theId) + item.getRownumber() + "");
        viewHolder.tv_title.setText(context.getString(R.string.theResult) + item.getGroupcount());
        viewHolder.buttonSetOnclick(new pViewHolder.ButtonTouchEvent() {
            @Override
            public void doubleClickSelect() {
//                Log.d(TAG, "doubleClickSelect: "+item.getGroupcount());
                cButtonTouchEvent.doubleClickSelect(item);
            }
        });
    }


    public interface CButtonTouchEvent {
        void doubleClickSelect(PeopleGroup.DataEntity peoleGroup);
    }

    /**
     * 按钮点击事件需要的方法
     */
    public void cButtonSetOnclick(CButtonTouchEvent cButtonTouchEvent) {
        this.cButtonTouchEvent = cButtonTouchEvent;
    }


    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

    static class pViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private TextView tv_title;
        private CardView cardView;
        public GestureDetectorCompat gestureDetectorCompat;
        private ButtonTouchEvent buttonTouchEvent;
        private MyGestureListener myGestureListener = new MyGestureListener();

        public pViewHolder(View itemView, Context context) {
            super(itemView);
//            Log.d(TAG, "DataAdapter: 5");
            textView = (TextView) itemView.findViewById(R.id.tv_id);
            cardView = (CardView) itemView.findViewById(R.id.card);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            gestureDetectorCompat = new GestureDetectorCompat(context, new MyGestureListener());
            cardView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    gestureDetectorCompat.onTouchEvent(event);
                    return true;
                }
            });
        }


        public class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
            private String TAG = MyGestureListener.class.getSimpleName();

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                buttonTouchEvent.doubleClickSelect();
                return false;
            }


        }


        public void buttonSetOnclick(ButtonTouchEvent buttonTouchEvent) {
            this.buttonTouchEvent = buttonTouchEvent;
        }

        /**
         * 按钮点击事件需要的方法
         */
        public interface ButtonTouchEvent {
            void doubleClickSelect();
        }
    }

}